<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" :disabled="true" style="width: 300px;" />
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input v-model="form.phone" style="width: 300px;" disabled/>
      </el-form-item>
      <el-form-item label="关联的代理商" prop="agentId">
          <el-select v-model="form.agentId" style="width: 300px" 
                    placeholder="请选择代理商" @change="getAgent" clearable filterable>
              <el-option
              v-for="(item, index) in users"
              :key="item.phone + index"
              :label="item.phone"
              :value="item.id"
              />
          </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getAllAgent } from '@/api/agentMember'
import { add, edit } from '@/api/yxUser'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      users: [],
      loading: false, dialog: false,
      form: {
        uid: '',
        account: '',
        pwd: '',
        username: '',
        freeTimes: 0,
        realName: '',
        birthday: '',
        cardId: '',
        mark: '',
        partnerId: '',
        groupId: '',
        nickname: '',
        avatar: '',
        phone: '',
        addTime: '',
        addIp: '',
        lastTime: '',
        lastIp: '',
        nowMoney: '',
        brokeragePrice: '',
        integral: '',
        signNum: '',
        status: '',
        level: '',
        spreadUid: '',
        spreadTime: '',
        userType: '',
        isPromoter: 0,
        payCount: '',
        spreadCount: '',
        cleanTime: '',
        addres: '',
        adminid: 0,
        loginType: ''
      },
      rules: {
          freeTimes: [
              { required: true, message: '请输入免费次数', trigger: 'blur' },
              { min: 1, max: 8, message: '长度在 1 到 40 个字符', trigger: 'blur' }
          ]
      }   
    }
  },
  mounted:function(){ //在页面加载参数结束后执行某个函数
      this.$nextTick(function(){
          //调用需要执行的方法
          this.getAgent()
      })      
  },
  methods: {
    getAgent() {
        getAllAgent().then(res => {
            this.users = res
        }).catch(() => { })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        uid: '',
        account: '',
        pwd: '',
        realName: '',
        birthday: '',
        cardId: '',
        mark: '',
        partnerId: '',
        groupId: '',
        nickname: '',
        avatar: '',
        phone: '',
        addTime: '',
        addIp: '',
        lastTime: '',
        lastIp: '',
        nowMoney: '',
        brokeragePrice: '',
        integral: '',
        signNum: '',
        status: '',
        level: '',
        spreadUid: '',
        spreadTime: '',
        userType: '',
        isPromoter: '',
        payCount: '',
        spreadCount: '',
        cleanTime: '',
        addres: '',
        adminid: '',
        loginType: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
